/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.core.wicket.panel.grid.column;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortStateLocator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.Loop.LoopItem;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;

/**
 * <pre>
 * 
 * 操作列
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public abstract class OperateColumn<T> implements IColumnHeader<T> {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7610559031393553352L;

	protected final int iterations;
	protected final String header;
	protected final ColumnLocation location;

	public OperateColumn(int iterations, ColumnLocation location) {
		this(iterations, "基本操作", location);
	}

	public OperateColumn(int iterations, String header, ColumnLocation location) {
		this.header = header;
		this.location = location;
		this.iterations = iterations;
	}

	public String getHeader() {
		return header;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator
	 * #populateItem(org.apache.wicket.markup.repeater.Item, java.lang.String,
	 * org.apache.wicket.model.IModel)
	 */
	@Override
	public void populateItem(Item<ICellPopulator<T>> cellItem, String componentId, IModel<T> rowModel) {
		cellItem.add(new OperatePanel<T>(componentId, this.iterations, rowModel) {

			private static final long serialVersionUID = -8073441989527805746L;

			@Override
			public void populateItem(String id, String labelId, LoopItem item, IModel<T> model) {
				OperateColumn.this.populateItem(id, labelId, item, model);
			}

		});
	}

	public abstract void populateItem(String id, String labelId, LoopItem item, IModel<T> model);

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.core.wicket.panel.grid.column.IColumnHeader#isAllowSort()
	 */
	@Override
	public boolean isAllowSort() {
		return false;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.core.wicket.panel.grid.column.IColumnHeader#getLocation()
	 */
	@Override
	public ColumnLocation getLocation() {
		return location;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.core.wicket.panel.grid.column.IColumnHeader#newHeader
	 * (org.apache.wicket.MarkupContainer, java.lang.String)
	 */
	@Override
	public Component newHeader(MarkupContainer parent, String id) {
		return new Label(id, getHeader());
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.core.wicket.panel.grid.column.IColumnHeader#newSortHeader
	 * (org.apache.wicket.MarkupContainer, java.lang.String,
	 * org.apache.wicket.extensions
	 * .markup.html.repeater.data.sort.ISortStateLocator)
	 */
	@Override
	public Component newSortHeader(MarkupContainer parent, String id, ISortStateLocator dataProvider) {
		return null;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.apache.wicket.model.IDetachable#detach()
	 */
	@Override
	public void detach() {

	}
}
